#delimit;
set more off;

set mem 200m;

set logtype text;

log using /home/dnc2101/Accidental_Deaths/Vital_Stats/Summary_Stats_Types_Accidents/make_where_pronounced_dead.log, replace;




/********************************************************************************************************************/
*
* This do file takes the Vital Stats Mortality data, and creates a dataset that has counts of the numbers of fatal 
* accidents, other than drug overdoses and auto accidents, that occur amongst persons pronounced death outside of
* hospitals and those pronounced death at hospitals in each state in each year.  These counts can be used along with 
* Census data on state-year populations to generate the rate of accidental deaths occuring outside of hospitals 
* in each state in each year.  
*
*
* Program by Dan Carvell, written between Fall 2008 and Spring 2010.  
*
*********************************************************************************************************************/







** Open up the dataset that already has dropped deaths due to auto accidents and drug overdoses, as well as intentional injuries, ;
** as I do not want to include those types of deaths in these variables ;
** that dataset was generated by the make_age_group_count_vars.do do file;

use "/home/dnc2101/Accidental_Deaths/Vital_Stats/Data_For_Appending/accidents_no_auto_no_OD_81_98.dta", clear;


gen at_hosp_count=0;
replace at_hosp_count=1 if ( (hospstat==6 | hospstat==7) & (year>=1981 & year<=1988) );
replace at_hosp_count=1 if ( (placdth==5 | placdth==6 | placdth==7) & (year>=1989 & year<=1998) );

tab at_hosp_count;
sum at_hosp_count;


gen not_at_hosp_count=0;
replace not_at_hosp_count=1 if ( (hospstat==1 | hospstat==2 | hospstat==3 | hospstat==4 | hospstat==5 | hospstat==8) & (year>=1981 & year<=1988) );
replace not_at_hosp_count=1 if ( (placdth==1 | placdth==2 | placdth==3 | placdth==4 ) & (year>=1989 & year<=1998) );

tab not_at_hosp_count;
sum not_at_hosp_count;




collapse (sum) at_hosp_count not_at_hosp_count, by(statefip year); 

summarize;

tab year;
tab statefip;

compress;


** sort the data so that the merge with other data goes OK;

sort statefip year;

** now save the data;

save "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/in_out_hospital_counts_81_98.dta", replace;








** Now do this with the data from 1999-2004, and then append the two datasets together;


use "/home/dnc2101/Accidental_Deaths/Vital_Stats/Data_For_Appending/accidents_no_auto_no_OD_99_04.dta", clear;

gen at_hosp_count=0;
replace at_hosp_count=1 if ( (placdth==4 | placdth==5 | placdth==6 | placdth==7) & (year>=2003 & year<=2004) );
replace at_hosp_count=1 if ( (placdth==5 | placdth==6 | placdth==7) & (year>=1999 & year<=2002) );

tab at_hosp_count;
sum at_hosp_count;


gen not_at_hosp_count=0;
replace not_at_hosp_count=1 if ( (placdth==1 | placdth==2 | placdth==3 ) & (year>=2003 & year<=2004 ) );
replace not_at_hosp_count=1 if ( (placdth==1 | placdth==2 | placdth==3 | placdth==4 ) & (year>=1999 & year<=2002) );

tab not_at_hosp_count;
sum not_at_hosp_count;






collapse (sum) at_hosp_count not_at_hosp_count, by(statefip year); 

summarize;


tab year;
tab statefip;

compress;


** sort the data so that the merge with other data goes OK;

sort statefip year;

** now save the data;

save "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/in_out_hospital_counts_99_04.dta", replace;



** Now, time to Append the 1981-1998 and 1999-2004 data together;


use "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/in_out_hospital_counts_81_98.dta", clear;

sort statefip year;

append using "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/in_out_hospital_counts_99_04.dta";

sort statefip year;

summarize;

save "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/in_out_hospital_counts_81_04.dta", replace;


xmlsave "/home/dnc2101/Accidental_Deaths/Vital_Stats/Cleaned_Datasets/in_out_hospital_counts_81_04.xml", doctype(excel) replace;







log close;

